package com.pxst.aop;

import java.lang.reflect.Method;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import com.pxst.annotations.EarlyWarn;
import com.pxst.earlywarn.entity.SysEarlywarnEntity;
import com.pxst.earlywarn.service.SysEarlywarnService;

import lombok.extern.slf4j.Slf4j;

/** 
* @author yangxy
* @version 创建时间：2023年9月1日 上午10:53:08 
*/
@Slf4j
@Aspect
@Order(-1)
@Component
public class EarlyWwarnAop {
	@Autowired
	private SysEarlywarnService sysEarlywarnService;
	
	@Pointcut("@annotation(com.pxst.annotations.EarlyWarn)")
	public void aopPoint() {
	}
    
	@AfterThrowing(pointcut = "aopPoint()",throwing = "ex")
	public void afterThrowing(JoinPoint pj, Exception ex) throws Exception {
		MethodSignature methodSignature = (MethodSignature) pj.getSignature();
		Method method = methodSignature.getMethod();
		EarlyWarn earlyWwarn = method.getAnnotation(EarlyWarn.class);
		SysEarlywarnEntity sysEarlywarnEntity = new SysEarlywarnEntity();
		sysEarlywarnEntity.setContent(earlyWwarn.content());
		sysEarlywarnEntity.setErrMsg(ex.getMessage());
		sysEarlywarnService.save(sysEarlywarnEntity);
	}
}
